// import Vue from 'vue'
export default {
  install(Vue) {
    const that = Vue.prototype
    /*
        数组分割
        参数 数组 分割间隔
    */
    that.split = (arr, count) => {
      let result = []
      for (let i = 0; i < arr.length; i += count) {
        result.push(arr.slice(i, i + count))
      }
      return result
    }
    /*
        数组去重
        参数 数组
    */
    that.unique = (arr) => {
      return [...new Set(arr)];
    }
    /*
        数组取某一项最大值
        参数 数组 元素
    */
    that.MaxParam = (arr, param) => {
      if (arr.length > 0) {
        let max = arr[0][param];
        for (let i = 1, len = arr.length; i < len; i++) {
          if (arr[i][param] > max) {
            max = arr[i][param]
          }
        }
        return max
      }
      else {
        return 0
      }
    }
    /*
           数组取某一项最小值
           参数 数组 元素
       */
    that.MinParam = (arr, param) => {
      if (arr.length > 0) {
        let min = arr[0][param];
        for (let i = 1, len = arr.length; i < len; i++) {
          if (arr[i][param] < min) {
            min = arr[i][param]
          }
        }
        return min
      } else {
        return 0
      }
    }
    //获取安卓版本号
    that.get_android_version = () => {
      const ua = navigator.userAgent.toLowerCase();
      let version = null;
      if (ua.indexOf("android") > 0) {
        let reg = /android [\d._]+/gi,
          v_info = ua.match(reg);
        version = (v_info + "").replace(/[^0-9|_.]/ig, "").replace(/_/ig, ".")
        version = parseInt(version.split('.')[0])
      }
      return version
    }
    //获取用户静态资源是否为本地
    that.isCdvfile = () => {
      return !/^[0-9]+$/.test(that.deviceUuid) && !that.variable.isMp
    }
    //async错误处理
    that.awaitWarp = (promise) => {
      return promise.then(data => [data, null]).catch(err => [null, err])
    }
    //用户更新数据接口
    that.userSaveDate = async (key, value) => {
      const json = {
        deviceUuid: that.deviceUuid,
        key: key,
        value: value
      }
      const [data, err] = await that.awaitWarp(that.$post(that.api.saveUserDate, json))
      err ? console.log("更新用户数据失败" + err) : console.log("更新用户数据成功")
    }
    //保存任务完成结果
    that.addAnswer = async (param) => {
      const json = {
        deviceUuid: that.deviceUuid,
        questionId: param.id,
        correct: param.type
      }
      const [data, err] = await that.awaitWarp(that.$post(that.api.addAnswer, json))
      err ? console.log("提交任务完成结果失败" + err) : console.log("提交任务完成结果")
    }
    //社区绘本初始化
    that.initRecPicbookList = async () => {
      const [data, err] = await that.awaitWarp(that.$post(that.api.recPicbookList))
      if (err) console.log("获取社区列表失败" + err)
      let arr = []
      for (let i = 0, len = data.picbooks.length; i < len; i++) {
        const list = [],
          content = JSON.parse(data.picbooks[i].content)
        for (let j = 0; j < content.pages; j++) {
          list.push({
            background: content.pageBg[j],
            imagery: content.imagery.filter(item => { return item.page === j }),
            role: content.role.filter(item => { return item.page === j }),
            wordAudio: content.wordAudio.filter(item => { return item.page === j })
          })
        }
        arr.push({
          deviceUuid: content.deviceUuid,
          id: data.picbooks[i].id,
          personalLevel: content.personalLevel,
          name: content.name,
          personalName: content.personalName,
          content: list,
          magicTiger: content.magicTigger,
          collectedCount: data.picbooks[i].collectedCount,
          imagery: content.imagery,
          role: content.role,
          wordAudio: content.wordAudio,
          pages: content.pages,
          pageBg: content.pageBg
        })
      }
      return arr
    }
    that.eachDescendentParameters = (option) => {
      var resultOption = $.extend({
        object: undefined,
        onEach: (map, key, value) => { }
      }, option)
      const onEach = resultOption.onEach
      doEachInner(null, null, resultOption.object)

      function doEachInner(mapInner, keyInner, valueInner) {
        // eslint-disable-next-line no-undef
        if (valueInner instanceof Array) {
          $.each(valueInner, function (index, item) {
            doEachInner(null, null, item)
          })
          // eslint-disable-next-line no-undef
        } else if (valueInner instanceof Object) {
          $.each(valueInner, function (key, value) {
            doEachInner(valueInner, key, value)
          })
        } else if (mapInner != null && keyInner != null) {
          onEach(mapInner, keyInner, valueInner)
        }
      }
    }
    that.parseTextAudio = async (option) => {
      const resultOption = $.extend({
        "text": "",
        "speaker": "Aiwei",
        "speed": 20,
        "pitch": 0,
        "volume": 100,
        "start": null,
        "end": null,
        "play": true
      }, option)
      if (resultOption.play) {
        const [data, err] = await that.awaitWarp(that.$post(that.api.parseTextAudio, resultOption))
        if (err) console.log("获取音频失败" + err)
        that.parseTextMusic({
          src: data.audioUrl,
          start: resultOption.start,
          end: resultOption.end
        })
      }
      else {
        that.parseTextMusic({
          play: false
        })
      }
    }
    that.initWindow = (option) => {
      const resultOption = $.extend({
        "windowWidth": 0,
        "windowHeight": 0,
        "mainWidth": 0,
        "mainHeight": 0,	
        "fontSize": 0
      }, option)
      for (const index in resultOption) {
        that[index] = resultOption[index]
      }
    }
  }
}